package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import componenti.statistica.CoppiaIperesponenziale;
import componenti.statistica.LeggeDistribuzione;

@SuppressWarnings("serial")
public class PCPanel extends JPanel {

	public final static int PRODUTTORE = 0, CONSUMATORE = 1;
	private JPanel upPanel;
	private JPanel downPanel;
	private JComboBox comboBox;
	private int TYPE;
	private double somma;
	JFormattedTextField lambdaTx;
	JFormattedTextField numeroFunzioniTx;
	JFormattedTextField mediaTx;
	JFormattedTextField varianzaTx;
	JLabel lambda;
	JLabel numeroFunzioni;
	JLabel media;
	JLabel varianza;
	Map<JButton, CoppiaIperesponenziale> mappa;

	LinkedList<CoppiaIperesponenziale> funzioni;

	public PCPanel(int TYPE) {

		this.TYPE = TYPE;
		this.setLayout(new GridBagLayout());
		mappa = new java.util.HashMap<JButton, CoppiaIperesponenziale>();
		initUpPanel();
		initDownPanel();

	}

	private void initUpPanel() {
		upPanel = new JPanel(new GridBagLayout());
		this.add(upPanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		comboBox = new JComboBox(LeggeDistribuzione.Tipi.values());
		upPanel.add(comboBox, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

	}

	private void initDownPanel() {
		downPanel = new JPanel(new GridBagLayout());
		this.add(downPanel, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		lambda = new JLabel("Lambda");
		lambdaTx = new JFormattedTextField(new RegexFormatter(
				"\\d+((.\\d){1}\\d?)?"));
		lambdaTx.setColumns(4);

		downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));
		downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 10, 5, 2), 0, 0));

		comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				switch ((LeggeDistribuzione.Tipi) comboBox.getSelectedItem()) {

				case ESPONENZIALE:
					downPanel.removeAll();

					lambda = new JLabel("Lambda");
					lambdaTx = new JFormattedTextField(new RegexFormatter(
							"\\d+((.\\d){1}\\d?)?"));
					lambdaTx.setColumns(4);

					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					((JDialog) PCPanel.this.getParent().getParent().getParent()
							.getParent().getParent().getParent()).pack();
					break;

				case ERLANG:
					downPanel.removeAll();

					lambda = new JLabel("Lambda");
					lambdaTx = new JFormattedTextField(new RegexFormatter(
							"\\d+((.\\d){1}\\d?)?"));
					lambdaTx.setColumns(4);

					numeroFunzioni = new JLabel("Numero Funzioni");
					numeroFunzioniTx = new JFormattedTextField(
							new RegexFormatter("[0123456789]*"));
					numeroFunzioniTx.setColumns(4);

					downPanel.add(lambda, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(lambdaTx, new GridBagConstraints(1, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));

					downPanel.add(numeroFunzioni, new GridBagConstraints(0, 3,
							1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(numeroFunzioniTx, new GridBagConstraints(1,
							3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					((JDialog) PCPanel.this.getParent().getParent().getParent()
							.getParent().getParent().getParent()).pack();
					break;

				case IPERESPONENZIALE:
					downPanel.removeAll();

					funzioni = new LinkedList<CoppiaIperesponenziale>();

					JButton aggiungFunzione = new JButton("Aggiungi Funzione");

					downPanel.add(aggiungFunzione, new GridBagConstraints(0, 3,
							1, 1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));

					aggiungFunzione.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent arg0) {
							new DialogAggiungiFunzione(PCPanel.this, funzioni,
									((JDialog) PCPanel.this.getParent()
											.getParent().getParent()
											.getParent().getParent()
											.getParent()));

							int i = 4;
							for (CoppiaIperesponenziale c : funzioni) {
								String lambdaText = c.getLambdaExp() + "";
								String alfaText = c.getAlfa() + "";
								final JLabel funzione = new JLabel(i - 3 + ") "
										+ "lambda = " + lambdaText
										+ "   alfa = " + alfaText);
								final JButton rimuovi = new JButton("Rimuovi");

								mappa.put(rimuovi, c);

								downPanel.add(funzione, new GridBagConstraints(
										0, i, 1, 1, 1.0, 1.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.BOTH, new Insets(10,
												10, 5, 2), 0, 0));

								downPanel.add(rimuovi, new GridBagConstraints(
										1, i, 1, 1, 1.0, 1.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.BOTH, new Insets(10,
												10, 5, 2), 0, 0));
								i++;

								((JDialog) PCPanel.this.getParent().getParent()
										.getParent().getParent().getParent()
										.getParent()).pack();

								rimuovi.addActionListener(new ActionListener() {

									@Override
									public void actionPerformed(ActionEvent arg0) {
										// PROBLEMIIIIIIIII
										for (JButton b : mappa.keySet()) {
											if (b.equals(rimuovi)) {
												funzioni.remove(mappa.get(b));
												mappa.remove(b);
												downPanel.remove(funzione);
												downPanel.remove(rimuovi);
												((JDialog) PCPanel.this
														.getParent()
														.getParent()
														.getParent()
														.getParent()
														.getParent()
														.getParent()).pack();

											}
										}

									}
								});
							}
						}

					});

					((JDialog) PCPanel.this.getParent().getParent().getParent()
							.getParent().getParent().getParent()).pack();
					somma = 0;
					break;

				case NORMALE:
					downPanel.removeAll();

					media = new JLabel("Media");
					varianza = new JLabel("Varianza");

					mediaTx = new JFormattedTextField(new RegexFormatter(
							"\\d+((.\\d){1}\\d?)?"));
					mediaTx.setColumns(4);

					varianzaTx = new JFormattedTextField(new RegexFormatter(
							"\\d+((.\\d){1}\\d?)?"));
					varianzaTx.setColumns(4);

					downPanel.add(media, new GridBagConstraints(0, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(mediaTx, new GridBagConstraints(1, 2, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(varianza, new GridBagConstraints(0, 3, 1, 1,
							1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					downPanel.add(varianzaTx, new GridBagConstraints(1, 3, 1,
							1, 1.0, 1.0, GridBagConstraints.CENTER,
							GridBagConstraints.BOTH, new Insets(10, 10, 5, 2),
							0, 0));
					((JDialog) PCPanel.this.getParent().getParent().getParent()
							.getParent().getParent().getParent()).pack();
					break;

				}
			}
		});

	}

	public LeggeDistribuzione.Tipi getSelection() {
		return (LeggeDistribuzione.Tipi) comboBox.getSelectedItem();
	}

	public double getSomma() {
		return somma;
	}

	public LinkedList<CoppiaIperesponenziale> getFunzioni() {
		if (funzioni.size() == 0) {
			if (TYPE == PRODUTTORE) {
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci le funzioni del produttore", "Errore",
						JOptionPane.ERROR_MESSAGE);
			} else
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci le funzioni del consumatore", "Errore",
						JOptionPane.ERROR_MESSAGE);
		} else {
			return funzioni;
		}
		return null;
	}

	public double getLambda() {
		if (lambdaTx.getText().equals("")) {
			if (TYPE == PRODUTTORE) {
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore di lambda per il produttore",
						"Errore", JOptionPane.ERROR_MESSAGE);
			} else
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore di lambda per il consumatore",
						"Errore", JOptionPane.ERROR_MESSAGE);
		} else {
			String result = sostituisciVirgola(lambdaTx.getText());
			return Double.parseDouble(result);
		}
		return -1;
	}

	public int getNumeroFunzioni() {
		if (numeroFunzioniTx.getText().equals("")) {
			if (TYPE == PRODUTTORE) {
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il numero di funzioni del produttore",
						"Errore", JOptionPane.ERROR_MESSAGE);
			} else
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il numero di funzioni del consumatore",
						"Errore", JOptionPane.ERROR_MESSAGE);
		} else {
			String result = sostituisciVirgola(numeroFunzioniTx.getText());
			return Integer.parseInt(result);
		}
		return -1;
	}

	public double getMedia() {
		if (mediaTx.getText().equals("")) {
			if (TYPE == PRODUTTORE) {
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore della media del produttore",
						"Errore", JOptionPane.ERROR_MESSAGE);
			} else
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore della media del consumatore",
						"Errore", JOptionPane.ERROR_MESSAGE);
		} else {
			String result = sostituisciVirgola(mediaTx.getText());
			return Double.parseDouble(result);
		}
		return -1;
	}

	public double getVarianza() {
		if (mediaTx.getText().equals("")) {
			if (TYPE == PRODUTTORE) {
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore della varianza del produttore",
						"Errore", JOptionPane.ERROR_MESSAGE);
			} else
				JOptionPane.showMessageDialog(PCPanel.this,
						"Inserisci il valore della varianza del consumatore",
						"Errore", JOptionPane.ERROR_MESSAGE);
		} else {
			String result = sostituisciVirgola(varianzaTx.getText());
			return Double.parseDouble(result);
		}
		return -1;
	}

	public void setSomma(double somma) {
		this.somma = somma;
	}

	private String sostituisciVirgola(String s) {
		char[] x = s.toCharArray();
		for (int i = 0; i < x.length; i++) {
			if (x[i] == ',') {
				x[i] = '.';
			}
		}
		return new String(x);
	}

}
